package com.iretailer.report.web.system;

import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.ReSendRegularly;
import com.iretailer.report.model.SendRegularly;
import com.iretailer.report.model.SendRegularlyLog;
import com.iretailer.report.model.User;
import com.iretailer.report.quartz.sendReqularly.SendRegularlyCronService;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.SecurityUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/5/30 10:56
 * @Description:
 */
@RestController
public class SendRegularlyController extends BaseAction {
    @Autowired
    private SendRegularlyCronService sendRegularlyCronService;

    @RequestMapping("/system/gridSendRegularly.action")
    public Map<String, Object> grid(@RequestBody ReSendRegularly reSendRegularly, HttpServletRequest request) {
         User user = SecurityUserUtil.currentUser(request);
        QueryOption queryOption = modelMapper.map(reSendRegularly, QueryOption.class);
        String companyId = user.getCompanyId();
        if (companyId.equals("-")) {
            companyId = null;
        }
        PageResult<SendRegularly> result = sendRegularlyService.grid(companyId, reSendRegularly.getEnabled(), reSendRegularly.getCompanyName(), queryOption);
        return RMap.ok(result);
    }

    @RequestMapping("/system/gridSendRegularlyLogs.action")
    public Map<String, Object> gridLogs(@RequestBody ReSendRegularly reSendRegularly) {
        QueryOption queryOption = modelMapper.map(reSendRegularly, QueryOption.class);
        PageResult<SendRegularlyLog> result = sendRegularlyService.gridLogs(reSendRegularly.getSendRegularlyId(), queryOption);
        return RMap.ok(result);
    }

    @RequestMapping("/system/saveOrUpdateSendRegularly.action")
    public Map<String, Object> saveOrUpdate(@RequestBody ReSendRegularly reSendRegularly,HttpServletRequest request) {
         User user = SecurityUserUtil.currentUser(request);
        SendRegularly insert = modelMapper.map(reSendRegularly, SendRegularly.class);
        boolean result = sendRegularlyService.saveOrUpdate(insert, user.getName());
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/system/saveSendRegularlyMalls.action")
    public Map<String, Object> saveSendRegularlyMalls(@RequestBody ReSendRegularly reSendRegularly) {
        boolean result = sendRegularlyService.insertSendRegularlyMalls(reSendRegularly.getId(), reSendRegularly.getMallIds());
        return RMap.resultBoolean(result);
    }


    @RequestMapping("/system/delSendRegularly.action")
    public Map<String, Object> del(@RequestBody List<String> ids) {
        boolean result = sendRegularlyService.deleteByIds(ids);
        return RMap.resultBoolean(result);
    }

    @RequestMapping("/system/uploadBySendRegulaylyIdAndTime.action")
    public Map<String, Object> uploadBySendRegulaylyIdAndTime(@RequestBody ReSendRegularly re) {
        boolean result = sendRegularlyCronService.uploadByTime(re.getId(), re.getStartTime(), re.getEndTime(),re.getIncludeEnd());
        return RMap.ok(result);
    }

    @RequestMapping("/system/uploadBySendRegulaylyLog.action")
    public Map<String, Object> uploadBySendRegulaylyLog(@RequestBody ReSendRegularly reSendRegularly) {
        boolean result = sendRegularlyCronService.uploadByLog(reSendRegularly.getSendRegularlyLog());
        return RMap.ok(result);
    }

}
